

<%@page import="multiface.web.util.Util"%>
<%@page import="multiface.valentinorusso.jpa.Lavoro"%>
<%@page import="java.util.List"%>
<%@page import="multiface.valentinorusso.jpa.Categoria"%>
<%@page import="multiface.web.util.Paginator"%>
<%@page import="javax.persistence.Query"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="multiface.valentinorusso.jpa.util.PersistenceManager"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%
    String pag = request.getParameter("page");
    String sez = request.getParameter("sez");
    String text = request.getParameter("text");
    String action = request.getParameter("action");
    String cid = request.getParameter("cid");
    if (action == null) {
        action = "";
    }
    if (sez == null) {
        sez = "lavorazioni";
    }
    String cids = "";
    if (cid != null) {
        cids = "&cid=" + cid;
    }
%>

<%
    // ######## PAGINATOR ########

    //Parametri della jsp
    String jsp = "index.jsp";
    String indexs = request.getParameter("index");
    int index = 0;

    //Controlli sui parametri
    if (indexs != null) {
        index = Integer.parseInt(indexs);
    }

    // Costruttore Paginator
    Paginator paginator = new Paginator();
    paginator.setCSSClass("pager-list");
    paginator.setCSSClassCurrentPage("selected");
    paginator.setCurrentIndex(index);
    paginator.setElementsForPage_fromDB(true);
    paginator.setPageReturn(jsp);

    // add Parameter
    if (!action.equals("")) {
        paginator.addParameter("action", action);
    }

    paginator.addParameter("page", pag);
    paginator.addParameter("sez", sez);
    paginator.addParameter("adm", session.getId());
%>
<%
    EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
    EntityManager em = emf.createEntityManager();

    String title = "";
    if (cid != null) {
        Categoria c = em.find(Categoria.class, Integer.parseInt(cid));
        title = " di " + c.getNome();
    }

    Query q = em.createNativeQuery("SELECT * FROM lavoro ORDER BY dataora DESC", Lavoro.class);

    if (text==null && cid != null) {
        q = em.createNativeQuery("SELECT * FROM lavoro WHERE categoria=? ORDER BY dataora DESC", Lavoro.class).setParameter(1, Integer.parseInt(cid));
        paginator.addParameter("cid", cid);
    }
    
    if (text != null && cid == null) {
        q = em.createNativeQuery("SELECT * FROM lavoro WHERE nome LIKE ? ORDER BY dataora DESC", Lavoro.class);
        q.setParameter(1, "%" + text + "%");
        paginator.addParameter("text", text);
    }
    if (text != null && cid!=null) {
        q = em.createNativeQuery("SELECT * FROM lavoro WHERE nome LIKE ? AND categoria=? ORDER BY dataora DESC", Lavoro.class);
        q.setParameter(1, "%" + text + "%");
        q.setParameter(2, Integer.parseInt(cid));
        paginator.addParameter("text", text);
        paginator.addParameter("cid", cid);
    }

    paginator.setTotalElements(q.getResultList().size());
    q.setFirstResult(index);
    q.setMaxResults(paginator.getElementsForPage());
    List<Lavoro> listalav = q.getResultList();
%>
<style>
    .plus{
        display: none;
        height: 16px;
        width: 360px;
        float: left;
        margin-left: 6px;

    }
    .plus img{
        cursor: pointer;
    }
    .plus_all{
        height: 20px;
        width: 300px;
        float: right;
        margin-left: 6px;

        text-align: right;
    }
    .plus_all a{
        text-decoration: none;
        color: white;
        font-size: 10px;
        margin-right: 6px;
    }
    .plus a{
        text-decoration: none;
        font-size: 10px;
        margin-right: 6px;
    }
    tr{
        border-bottom: 1px #ddd dotted;
    }
    tr:hover{
        border-bottom: 1px #ddd dotted;
        background-color: #ededed;
    }
    .table-form{

    }
</style>
<script>
    
    function del(id){
        if(confirm('Eliminare il lavoro ?')){
            $.post("ActionServlet", 
            {  
                adm:'<%=session.getId()%>',
                lid:id,
                action:'del_lavoro'
            },
            function(data){ 
                location.href= "index.jsp?adm=<%=session.getId()%>&page=<%=pag%>&sez=<%=sez%>&action=<%=action%>";
            });
        }
    }
    
</script>
<div id="content" style="margin-top: 6px;width: 620px;float: left;text-align: left;">
    <div class="title">
        LAVORI<%=title%>
        <div class="plus_all" id="plus_all">
            <a href="index.jsp?adm=<%=session.getId()%>&page=adm&sez=add_lavoro&action=add_lavoro<%=cids%>">
                <img src="engine/img/btn/add.png" height="16" width="16"></img> Nuovo    
            </a>
        </div>
    </div>
    <table class="table-form">
        <tr style="background: #ddd">
            <td style="width: 50%;font-weight: bold">Titolo</td>
            <td style="width: 20%;font-weight: bold;">Data</td>
            <td style="width: 20%;font-weight: bold;">Categoria</td>
            <td style="width: 5%;font-weight: bold;"> </td>
            <td style="width: 5%;font-weight: bold;"> </td>
        </tr>

        <%
            for (Lavoro l : listalav) {
                em.refresh(l);
        %>
        <tr>
            <td style="font-size: 11px;"><%=l.getNome()%> </td>
            <td style="font-size: 11px;"><%=Util.FormatData(l.getDataora())%></td>
            <td style="font-size: 11px;"><a style="color:#0099FF;" href="index.jsp?adm=<%=session.getId()%>&page=adm&sez=lavori&cid=<%=l.getCategoria().getIdcategoria()%> "><%=l.getCategoria().getNome()%></a></td>
            <td style="width: 40px"><a href="index.jsp?adm=<%=session.getId()%>&page=adm&sez=mod_lavoro&action=mod_lavoro&lid=<%=l.getIdlavoro()%>"><img src="engine/img/btn/edit.png" title="Modifica Lavoro"></a></td>
            <td style="width: 40px"><a href="javascript:del(<%=l.getIdlavoro()%>);"><img src="engine/img/btn/del.png" title="Elimina Lavoro"></a></td>
        </tr>
        <%
            }
        %>


    </table>
    <%=paginator%>
</div>
<%em.close();%>